// testGetOntologies.js
(function() {
    
    function getPageSource() {
        var httpObject = makeHttpObject();
        httpObject.open("GET", location.pathname, false);
        httpObject.send(null);
        if (httpObject.status == 200) {
            return httpObject.responseText;
        } else {
            return null;
        }
    }
    
    function getOntologies() {
        var request = new XMLHttpRequest();
        request.open("GET", "http://www.duncangrant.co.uk/ontologyRequest.php", false);
        document.getElementById("output").innerHTML = "LOADING <img src='ajax-loader.gif' />";
        request.send(null);
        if (request.status == 200) {
            // eval transforms the responseText from a string into an actual javascript object (JSON)
            displayOutput(eval(request.responseText));
        } else {
            document.getElementById("output").innerHTML = "ERROR";
        }
    }
    
    function displayOutput(jsonResponse) {
        var output = "";
        for (var i = 0; i < jsonResponse.length; i++) {
            output += "<div><b><p>" + jsonResponse[i].name + "</p><p>" + jsonResponse[i].namespace + "</p><p>" + jsonResponse[i].description + "</p>";
            for (var j = 0; j < jsonResponse[i].labels.length; j++) {
                output += "<p>" + jsonResponse[i].labels[j] + "</p>";
            }
        }
        document.getElementById("output").innerHTML = output;
    }
    
    window.onload = function() {
        getOntologies();
    };
    
})();